package rateexchange.client.mvp;

import rateexchange.client.ClientFactory;
import rateexchange.client.place.RatesTablePlace;
import rateexchange.client.ui.RatesTableView;
import rateexchange.client.ui.RatesTableView.Presenter;
import rateexchange.shared.RateRequest;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

//Activity driving the RatesTableView
public class RatesTableActivity extends AbstractActivity implements Presenter{
	private ClientFactory clientfactory;
	
	public RatesTableActivity(RatesTablePlace place, ClientFactory clientFactory){
		this.clientfactory = clientFactory;
	}
	
	
	@SuppressWarnings("deprecation")
	@Override
	//Setting up the driven View
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		final RatesTableView ratesView= clientfactory.getRatesTable();
		//Providing the dataprovider to the View
		RateDataProvider dataProvider = new RateDataProvider(clientfactory);	
		ratesView.setDataProvider(dataProvider);
		//Setting up the bidirectional communication between view and activity. 
		ratesView.setPresenter(this);
		RateRequest ratesreq = clientfactory.getRequestFactory().raterequest();
		ratesreq.countRates().fire(new Receiver<Integer>() {
			

			@Override
			public void onSuccess(Integer response) {
				ratesView.setRowCount(response);
				
			}
		});
	//Providing the view to the container associated with ActivityManager in Entry Point class
	panel.setWidget(ratesView.asWidget());	
	}

	@Override
	public void goTo(Place place) {
		clientfactory.getPlaceController().goTo(place);
		
	}

}
